package java_test.io;

import java.io.File;

public class FileTest3 {
    public static void main(String[] args) {
        //printSubFile(new File("C:\\Users\\lemon\\Desktop\\Desktop\\stduy"));
        //System.out.println(getDirectorySize(new File("C:\\Users\\lemon\\Desktop\\Desktop\\stduy")));
        //deleteDirectory(new File("C:\\Users\\lemon\\Desktop\\C++小游戏项目"));
    }

    /**
     * 递归实现
     */
    public static void printSubFile(File dir) {
        File[] files = dir.listFiles();
        for (File file : files) {
            if (file.isDirectory()) {
                printSubFile(file);
            } else {
                System.out.println(file.getAbsoluteFile());
            }
        }
    }

    //拓展一
    public static long getDirectorySize(File file) {
        File[] files = file.listFiles();
        long size = 0;
        for (File f : files) {
            if (f.isFile()) {
                size += f.length();
            } else {
                size += getDirectorySize(f);
            }
        }
        return size;
    }

    //拓展二
    public static void deleteDirectory(File file) {
        File[] files = file.listFiles();
        for (File f : files) {
            if (f.isFile()) {
                f.delete();
            } else {
                deleteDirectory(f);
            }
        }
        file.delete();
    }
}
